package baiwa.rta.service.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import baiwa.rta.dao.UserDao;
import baiwa.rta.model.DataTableModel;
import baiwa.rta.model.User;
import baiwa.rta.service.UserService;
import baiwa.rta.utils.CommonUtils;

@Service("UserService")
@Transactional(readOnly = false)
public class UserServiceImpl implements UserService {

	@Autowired UserDao dao;

	@Override
	@Transactional(readOnly = true)
	public List<User> findAll() {
		return dao.findAll();
	}

	@Override
	@Transactional(readOnly = true)
	public User findById(Integer id) {
		return dao.findById(id);
	}

	@Override
	public User findByUsername(String username) {
		return dao.findByUsername(username);
	}

	@Override
	public List<User> findUserByCriteria(User user) {
		return dao.findUserByCriteria(user);
	}

	@Override
	public DataTableModel<User> getUsersDataTable(User user, int iDisplayStart,
			int iDisplayLength) {
		return dao.findUserDataTable(user, iDisplayStart,
				iDisplayLength);
	}

    @Override
    @Transactional
    public void save(User user) {
        dao.create(user);
    }

    @Override
    @Transactional
    public void update(User user) {
        dao.update(user);
    }

    @Override
    @Transactional
    public void delete(User user) {
        dao.delete(user);
    }

    @Override
    @Transactional
    public boolean deleteByUsername(String username) throws Exception {
        
        User user = dao.findByUsername(username);
        
        if (!CommonUtils.isNullOrEmtry(user)) {
            dao.delete(user);
            return true;
        } else {
            return false;
        }
    }

	@Override
	public User findLoginUsername(String username) {
		return dao.findLoginUsername(username);
	}

}
